#coding=utf-8
mylist = [1, 4, -5, 10, -7, 2, 3, -1]
a = [n for n in mylist if n > 0]
b = [n for n in mylist if n < 0]
print(a)
print(b)

pos = (n for n in mylist if n > 0)
print(pos)
for x in pos:
    print(x)

#  put the filtering code into its own
# function and use the built-in  filter() function
values = ['1', '2', '-3', '-', '4', 'N/A', '5']
def is_int(val):
    try:
        x = int(val)
        return True
    except ValueError:
        return False

# filter() creates an iterator, so if you want to create a list of results, make sure you also
# use  list() as shown
print(list(filter(is_int, values)))
# Outputs ['1', '2', '-3', '4', '5']


## Part2
# List comprehensions and generator expressions are often the easiest and most straight‐
# forward ways to filter simple data. They also have the added power to transform the
# data at the same time.
mylist = [1, 4, -5, 10, -7, 2, 3, -1]
import math
[math.sqrt(n) for n in mylist if n > 0]

clip_neg = [n if n > 0 else 0 for n in mylist]
clip_pos = [n if n < 0 else 0 for n in mylist]
print(clip_neg)
print(clip_pos)


